import time
from datetime import datetime
import uuid

from sqlalchemy.dialects.postgresql import Any

from config.database import Base
from sqlalchemy import Column, Integer, String, TIMESTAMP, Boolean


def gen_uuid():
    timestamp = str(int(time.time()))

    return uuid.uuid5(uuid.NAMESPACE_DNS, str(timestamp) + str(uuid.uuid4()))


class Users(Base):
    """
    用户表
    """
    __tablename__ = 'tb_users'
    __table_args__ = {
        'schema': 'reviews'
    }

    user_id = Column(String(32), default=gen_uuid(), primary_key=True, comment='表索引主键')
    user_name = Column(String(26), nullable=False, comment='真实姓名')
    avatar = Column(String(255), comment='微信头像')
    nickname = Column(String(26), comment='微信昵称')
    password = Column(String(50), nullable=False, comment='密文密码')
    user_phone = Column(String(11), default='', comment='手机号码')
    student_so = Column(String(20), nullable=False, comment='学号')
    user_student_code = Column(String(100), default='', comment='学籍信息')
    is_blacklist = Column(Boolean, default=False, comment='是否在黑名单')
    registry_time = Column(TIMESTAMP, default=datetime.now().timestamp(), comment='注册时间')
    last_login_time = Column(TIMESTAMP, default=datetime.now().timestamp(), comment='最后登录时间')

    def __init__(self, **kw: Any):
        super().__init__(**kw)
        self.user_id = gen_uuid()
